package com.intramirror.product.schedule.consumer;

import com.intramirror.product.constant.ProductChangedConstant;
import com.intramirror.product.entity.Product;
import com.intramirror.product.entity.Spu;
import com.intramirror.product.service.FacilityService;
import com.intramirror.product.service.IAggregateService;
import com.intramirror.product.service.IProductService;
import com.intramirror.product.service.ISpuService;
import com.intramirror.product.utils.AggregateRuleManagement;
import com.intramirror.product.vo.ProductChangedVO;
import com.intramirror.utils.transform.JsonTransformUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by 123 on 2018/5/25.
 */
public class SpuUpdateConsumer implements IProductConsumer {
    private static final Logger LOGGER = LoggerFactory.getLogger(SnapshotConsumer.class);

    private FacilityService facilityService;

    SpuUpdateConsumer(FacilityService facilityService) {
        this.facilityService = facilityService;
    }

    @Override
    public void process(String message) {
        LOGGER.info("SpuUpdateConsumer process start to run.");
        //接收到商品更新的消息
        LOGGER.info("SpuUpdateConsumer----------dispatch message:{}", message);
        ProductChangedVO ProductChangedVO = JsonTransformUtil.readValue(message, ProductChangedVO.class);
        if (ProductChangedVO.getProduct_id() == null || ProductChangedVO.getType() == null) {
            LOGGER.error("SpuUpdateConsumer----------params validate error:{}", message);
            return;
        }

        ISpuService spuService = facilityService.getSpuService();
        IProductService productService = facilityService.getProductService();
        IAggregateService aggregateService = facilityService.getAggregateService();
        Product product = productService.selectByPrimaryKey(ProductChangedVO.getProduct_id());
        if (product == null) {
            LOGGER.error("SpuUpdateConsumer----------product is not exists. productId={}", ProductChangedVO.getProduct_id());
            return;
        }
        Long spuId = product.getSpuId();
        if (spuId == null) {
            LOGGER.info("SpuUpdateConsumer----------spu id is not null.");
            return;
        }
        Spu spu = spuService.getSpuById(spuId);
        if (ProductChangedVO.getType() == ProductChangedConstant.PRODUCT_UPDATE_BY_JOB_TYPE_IMG) {
            //按照Vendor Sort排序规则处理电商图
            if (vendorSortCheck(spu.getVendorId(), product.getVendorId())) {
                //更新电商图
                LOGGER.info("SpuUpdateConsumer----------updateBoutiqueSpuImg");
                aggregateService.updateBoutiqueSpuImg(product, spu);
            } else {
                LOGGER.info("SpuUpdateConsumer----------Product aggregate failed.Product Vendor Sort >= Spu Vendor Sort");
            }
        } else if(ProductChangedVO.getType() == ProductChangedConstant.PRODUCT_UPDATE_BY_JOB_TYPE_SEASON_CODE ||
                ProductChangedVO.getType() == ProductChangedConstant.PRODUCT_UPDATE_BY_JOB_TYPE_DESIGNER_ID_COLOR_CODE){
            LOGGER.info("SpuUpdateConsumer----------rebindingProductSpu spuId={}, productId={}", spuId, ProductChangedVO.getProduct_id());
            spuService.rebindingProductSpu(product, spu);
        }
    }

    private boolean vendorSortCheck(Long spuVendorId, Long productVendorId) {
        Map<Long, Integer> boutiqueImgSortMap = AggregateRuleManagement.getInstance().getBoutiqueImgSortMap();
        //找不到排序位置的vendor id则把优先级定成最低，不用它的图片
        Integer spuSort = boutiqueImgSortMap.get(spuVendorId) != null ? boutiqueImgSortMap.get(spuVendorId) : 10001;
        Integer prdSort = boutiqueImgSortMap.get(productVendorId) != null ? boutiqueImgSortMap.get(productVendorId) : 10000;
        return prdSort <= spuSort;
    }
}
